package huaweiod.first;

import java.util.HashMap;
import java.util.Map;

/**
 * 华为OD机试真题 Java 实现【计算快递业务主站点】【2022.11 Q4 新题】
 */
public class A_2023_44_ComputingMasterSite {

    public static int computingMasterSite(int[][] matrix) {
        int count = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < matrix.length; i++) {
            if (!map.containsKey(i)) {
                process(i, matrix, map);
                count++;
            }
        }
        return count;
    }

    public static void process(int index, int[][] matrix, Map<Integer, Integer> map) {
        for (int i = 0; i < matrix[index].length; i++) {
            if (matrix[index][i] == 1) {
                map.put(i, 1);
            }
        }
    }

    public static void main(String[] args) {
        int[][] matrix = {{1, 1, 1, 1}, {1, 1, 1, 0}, {1, 1, 1, 0}, {1, 0, 0, 1}};
        System.out.println(computingMasterSite(matrix));    // 1
        int[][] matrix1 = {{1, 1, 0, 0}, {1, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
        System.out.println(computingMasterSite(matrix1));   // 3
    }
}
